package com.huaiwei.usmManager.modules.usm.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.huaiwei.usmManager.common.utils.Constant;
import com.huaiwei.usmManager.common.utils.NumberUtils;
import com.huaiwei.usmManager.common.utils.PageUtils;
import com.huaiwei.usmManager.common.utils.Query;
import com.huaiwei.usmManager.modules.usm.dao.ShopDao;
import com.huaiwei.usmManager.modules.usm.entity.CabinetEntity;
import com.huaiwei.usmManager.modules.usm.entity.GoodsEntity;
import com.huaiwei.usmManager.modules.usm.entity.ResourceEntity;
import com.huaiwei.usmManager.modules.usm.entity.ShopEntity;
import com.huaiwei.usmManager.modules.usm.service.CabinetService;
import com.huaiwei.usmManager.modules.usm.service.GoodsService;
import com.huaiwei.usmManager.modules.usm.service.ResourceService;
import com.huaiwei.usmManager.modules.usm.service.ShopService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;




@Service
@Transactional
public class ShopServiceImpl extends ServiceImpl<ShopDao, ShopEntity> implements ShopService {

    @Autowired
    private ShopService shopService;
    @Autowired
    private CabinetService cabinetService;
    @Autowired
    private ShopDao shopDao;
    @Autowired
    private ResourceService resourceService;

    /*分页*/
    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        String shopName=(String)params.get("shopName");
        Integer status= NumberUtils.obj2Integer(params.get("status"));

        IPage<ShopEntity> page=this.page(new Query<ShopEntity>().getPage(params),
                new QueryWrapper<ShopEntity>()
                        .like(StringUtils.isNotBlank(shopName),"shop_name",shopName)
                        .eq(status!=null,"status",status)
        );
        return new PageUtils(page);

    }

    /*新增及修改店铺信息*/
    @Override
    public void insertOrUpdate(ShopEntity shopEntity) {
        if(shopEntity.getShopId()!=null){
            deleteRelationInfo(shopEntity.getShopId());/*删除关联信息完之后往下运行，更新即是新增*/
        }
        if(this.saveOrUpdate(shopEntity)){
            /*插入图片信息*/
            if(shopEntity.getImageUrls()!=null&&shopEntity.getImageUrls().length>0){
                /*数组转List*/
                List<ResourceEntity> imageList=new ArrayList<>();
                for(String image:shopEntity.getImageUrls()){
                    ResourceEntity imageResource=new ResourceEntity();
                    imageResource.setResourceId(shopEntity.getShopId());
                    imageResource.setType(Constant.SHOP);
                    imageResource.setImageUrl(image);
                    imageResource.setStatus(1);
                    imageList.add(imageResource);
                }
                /*批量添加图片*/
                resourceService.saveBatch(imageList);
            }

        }
    }

    /*根据Id获取店铺信息*/
    @Override
    public ShopEntity getInfoByShopId(Integer shopId) {
        /*拿到店铺的信息*/
        ShopEntity shopEntity=this.getById(shopId);
        List<ResourceEntity> imageList=getResourceListByShopId(shopId);
        shopEntity.setImageList(imageList);
        return shopEntity;
    }

    /*删除店铺信息*/
    @Override
    public void deleteInfo(Integer shopId) {
            if(this.removeById(shopId)){
            deleteRelationInfo(shopId);
        }
    }

    /*删除关联的信息*/
    public void deleteRelationInfo(Integer shopId){
        /*删除图片*/
        resourceService.remove(new QueryWrapper<ResourceEntity>()
                .eq("resource_id",shopId)
                .eq("type", Constant.SHOP)
                .eq("status",Constant.ON));
    }

    /*通过店铺Id查询店铺内是否有展示柜*/
    @Override
    public boolean findCabinetInShopByShopId(int shopId) {
        if(shopDao.findCabinetInShopByShopId(shopId)>0){
            return false;
        }else {
            return true;
        }
    }

    /*查询店铺的Resourcelist信息*/
    @Override
    public List<ResourceEntity> getResourceListByShopId(Integer shopId) {
        return resourceService.list(new QueryWrapper<ResourceEntity>()
                .eq("resource_id",shopId)
                .eq("type", Constant.SHOP)
                .eq("status",Constant.ON));
    }

    /*根据店铺Id查询店铺内的展示柜列表*/
    @Override
    public PageUtils getCabinetListByShopId(Integer shopId, Map<String, Object> params) {
        IPage<CabinetEntity> page=cabinetService.page(new Query<CabinetEntity>().getPage(params),
                new QueryWrapper<CabinetEntity>().eq("shop_id",shopId));
        return new PageUtils(page);
    }

    /*展示柜所需的方法*/
    @Override
    public List<ShopEntity> getAllShop() {
        List<ShopEntity> list = new ArrayList<>();
        list = this.list(new QueryWrapper<ShopEntity>());
        return list;
    }

}
